package ca.etsmtl.logti.log619.lab05.filter;

import java.util.Enumeration;

import javax.mail.MessagingException;
import javax.mail.internet.InternetHeaders;
import javax.mail.internet.MimeMessage;

public class UnverifiedFilter implements EmailFilter {

	final String NAME = "Unverified Filter";
	
	@Override
	public String getFilterName() {
		return this.NAME;
	}

	@Override
	public byte checkSpam(MimeMessage message) {
		
		byte spamValue = CANT_SAY;
		
		if(containsUnverified(message)) {
			
			spamValue = 100;
		}
		
		return spamValue;
	}

	private boolean containsUnverified(MimeMessage message) {
		
		String header;
		 
		try {
			
			Enumeration<String> headers = message.getAllHeaderLines();
			
			while ( headers.hasMoreElements() ) {
				
				header = (String) headers.nextElement();
				
				if( header.startsWith("Received:") && header.contains("(unverified") ){
					return true;
				}
				
				
			}
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		return false;
	}

	@Override
	public byte getConfidenceLevel() {

		return 100;
	}

	@Override
	public boolean enabled() {

		return true;
	}

}
